package homework2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = scan.nextLine().trim();
        System.out.println("请输入密码：");
        String pwd = scan.nextLine().trim();
        System.out.println("请输入昵称：");
        String nick = scan.nextLine().trim();
        System.out.println("请输入年龄：");
        int age = scan.nextInt();

        if (name.isEmpty() || pwd.isEmpty() || nick.isEmpty() ||
                !name.matches("[a-zA-Z0-9_]{1,32}")) {
            System.out.println("注册信息输出有误！");
            return;
        }
        User user = new User(name, pwd, nick, age);
        try(
            FileOutputStream fos = new FileOutputStream(name + ".obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(user);
            System.out.println("注册完毕！");
        }catch(IOException e) {
            e.printStackTrace();
        }

    }
}
